<?php

function reequipment_adminapi_createorder($args)
{
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return
    $invalid = array();
    if (!isset($part_number) || !is_string($part_number)) {
        $invalid[] = 'part_number';
    } 
    if (!isset($company_id) || !is_int($company_id)) {
        $invalid[] = 'company_id';
    } 
    if (!isset($man_part_number) || !is_string($man_part_number)) {
        $invalid[] = 'man_part_number';
    } 
    if (!isset($cost) || !is_float($cost)) {
        $invalid[] = 'cost';
    } 
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1)', join(', ', $invalid));
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    } 
    
    // Need to get the model id.
    if ($equipment_id != '0'){
		$model = xarModAPIFunc('reequipment', 'user', 'getequipment', array('equipment_id' => $equipment_id));
		foreach ($model as $var){
			$model_id = $var['model_id'];
			$cycles = $var['cycles'];
		}
	} else {
		$model_id = '0';
		$cycles = '0';
	}
    
    // Assume that when this is entered is when it was ordered.
    $last_ordered = time();
    $date_returned = strtotime($date_returned);
	$date_expected = strtotime($date_expected);
   
    /* Fields
    order_id,
	part_number,
	description	,
	model_id,
	equipment_id, 	 	
	company_id, 	 	
	last_ordered, 	 	
	date_expected,
	man_part_number,	 
	cost,
	agent,
	tracking_number,
	return_auth,	 	 
	credit_memo,	 
	date_returned
	*/
    
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reordered'];
    // Get next ID in table
    $nextId = $dbconn->GenId($table);

    // Add item
    $query = "INSERT INTO $table ("
        . "     order_id,
				part_number,
				description	,
				model_id,
				equipment_id, 	 	
				company_id, 	 	
				last_ordered, 	 	
				date_expected,
				man_part_number,	 
				cost,
				agent,
				tracking_number,
				return_auth,	 	 
				credit_memo,	 
				date_returned)"
        . " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $result =& $dbconn->Execute($query, array(
    			$nextId, 
    			$part_number,
				$description,
				$model_id,
				$equipment_id, 	 	
				$company_id, 	 	
				$last_ordered, 	 	
				$date_expected,
				$man_part_number,	 
				$cost,
				$agent,
				$tracking_number,
				$return_auth,	 	 
				$credit_memo,	 
				$date_returned));
    if (!$result) return;
    // Get the ID of the item that we inserted
    $id = $dbconn->PO_Insert_ID($table, 'order_id');

    // Let any hooks know that we have created a new forum
    $args['module'] = 'reequipment';
    $args['itemtype'] = 7; // post
    $args['itemid'] = $id; // post
    xarModCallHooks('item', 'create', $id, $args);// post
    // Return the id of the newly created link to the calling process
    return $id;
}
?>